package com.wgchao.authmanager.infrastructure.convert.em;

import com.wgchao.authmanager.domain.model.Administrator;
import com.wgchao.authmanager.domain.model.Operator;
import com.wgchao.authmanager.infrastructure.convert.Convert;
import com.wgchao.authmanager.infrastructure.jpa.entity.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @ClassName: UserInfoConvert
 * @author: wgchao
 * @createTime: 2020/4/29 3:29 PM
 * @Description: TODO
 */
@Component
public class UserInfoConvert implements Convert<Administrator, UserInfo> {

    @Override
    public UserInfo toEntity(Administrator administrator) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(administrator, userInfo);

        if(administrator.getCreator() != null){
            userInfo.setCreateUser(administrator.getCreator().getUname());
            userInfo.setCreateTime(administrator.getCreator().getOperateTime());
        }
        if(administrator.getLastupdator() != null){
            userInfo.setLastUpdateUser(administrator.getLastupdator().getUname());
            userInfo.setLastUpdateTime(administrator.getLastupdator().getOperateTime());
        }
        userInfo.setId(administrator.findId());
        userInfo.setVersion(administrator.findVersion());
        return userInfo;
    }

    @Override
    public Administrator toModel(UserInfo userInfo) {
        return new Administrator(userInfo.getId(), userInfo.getUname(), userInfo.getPassword(), userInfo.getName(),
                userInfo.getAgencyCode(), userInfo.getTel(), userInfo.getEmail(), userInfo.isDisabled(),
                new Operator(userInfo.getCreateUser(), userInfo.getCreateTime()),
                new Operator(userInfo.getLastUpdateUser(), userInfo.getLastUpdateTime()), userInfo.getVersion());
    }
}
